package com.yl.zqy.service.impl.sys;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.zqy.common.MemoryCache;
import com.yl.zqy.common.Result;
import com.yl.zqy.entity.sys.SysConfig;
import com.yl.zqy.mapper.sys.SysConfigMapper;
import com.yl.zqy.service.sys.SysConfigService;
import com.yl.zqy.utils.QueryUtil;
import com.yl.zqy.vo.common.IPageVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yl.zqy.constant.ZQYConstant.CACHE_TYPE_CONFIG;

/**
 * <p>
 * 参数管理 服务实现类
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-24
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
    private Map cache = MemoryCache.getInstance().getMemoryCacheMap();

    @Override
    public void initSysConfigParams() {
        List<SysConfig> sysConfigs = baseMapper.selectList(null);
        Map<String, Object> configMap = new HashMap<>();
        sysConfigs.stream().forEach(item -> {
            configMap.put(item.getParamCode(), item.getParamValue());
        });
        cache.put(CACHE_TYPE_CONFIG, configMap);
        log.info("系统变量参数，刷新到缓存成功 - {}", MemoryCache.getInstance().getMemoryCacheMap().get(CACHE_TYPE_CONFIG));
    }

    @Override
    public void refreshSysConfigParams() {
        cache.remove(CACHE_TYPE_CONFIG);
        initSysConfigParams();
    }

    @Override
    public Result<Boolean> insertSysConfig(SysConfig config) {
        int insert = baseMapper.insert(config);
        if (insert > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "新增失败！");
    }

    @Override
    public Result<Boolean> updateSysConfig(SysConfig config) {
        int update = baseMapper.updateById(config);
        if (update > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "修改失败！");
    }

    @Override
    public Result<Boolean> deleteSysConfigByIds(List<String> ids) {
        int result = baseMapper.deleteBatchIds(ids);
        if (result > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "操作失败！");
    }

    @Override
    public Result<IPageVo<SysConfig>> getListByPage(int current, int size, String query) {
        LambdaQueryWrapper<SysConfig> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.and(StringUtils.isNotBlank(query),c->c.like(SysConfig::getParamCode,query)
                .or()
                .like(SysConfig::getParamValue,query));
        Page<SysConfig> selectPage = baseMapper.selectPage(Page.of(current, size), queryWrapper);
        return Result.ok(QueryUtil.iPage2IPageVo(selectPage));
    }
}
